(function ($) {
    function DetailList(element, options) {
        this.dbPath = G_Path.path ? G_Path.path : "";
        this.element = element;
        this.contentElement;
        this.toolsElement;
        this.options = options;
        this.parentId = options.id;
        //this.goodsId = options.goodsId;
        this.fieldInfo = this.options.fieldInfo;
        this.formName = "list";
        this.formIndex = 0;
        this.tableElement = null;
        this.data = [];
        this.start = 1;
        this.rowSize = 50;

        this.winDialog = null;
        this.status = this.options.status || {ADD: 1, EDIT: 2, VIEW: 3};
        this.dialogStatus = this.options.dialogStatus || {ADD: 1, EDIT: 2, VIEW: 3};
        this.init();

    }

    $.extend(DetailList.prototype, {
        init: function () {
            var self = this;
            var tabel = document.getElementsByTagName("table");
            if (tabel.length > 0) {
                $("table").remove()
            }
            self.tableElement = document.createElement("table");
            self.tableElement.className = "tb-list table";
            //.tb-list td{border:none}
            var oRow = self.tableElement.insertRow(self.tableElement.rows.length);
            oRow.className = "tb_tr_title";
            this.contentElement = $("<div></div>").appendTo(this.element);
        },
        get: function (categoryId) {
            var self = this;
            var url = "/goods/cust/fields/json/" + categoryId;
            var requestData = this.options.param ? this.options.param : {};
            $.get(url, requestData, function (data) {
                var queryPage = $.parseJSON(data);
                $(self.contentElement).html("");
                if (queryPage && queryPage.list) {
                    self.data = queryPage.list;
                    self.draw("get");
                }

            });
        },
        getList: function () {
            var self = this;
            var url = "/goods/cust/fields/value/json/" + this.parentId;
            var requestData = this.options.param ? this.options.param : {};
            $.get(url, requestData, function (data) {
                var queryPage = $.parseJSON(data);
                $(self.contentElement).html("");
                if (queryPage && queryPage.list) {
                    self.data = queryPage.list;
                    self.draw("list");
                }

            });
        },
        draw: function (type) {
            var self = this;
            if (this.options.showDelete || this.options.showEdit) {
                var oCell = oRow.insertCell(oRow.cells.length);
                oCell.innerHTML = "";
            }
            //系统已经有数据，放数据
            //添加数据
            for (var i = 0; i < this.data.length; i++) {
                self.addRow(this.data[i], type);
            }
            $(this.contentElement).append(self.tableElement);


        },
        addRow: function (model, type) {
            var self = this;
            var oRow = self.tableElement.insertRow(self.tableElement.rows.length);
            var oCell = oRow.insertCell(oRow.cells.length);
            oCell.style.border = "none";
            var fieldValue = '<div class="form-group"><input type="hidden" name=list[' + this.formIndex + '].id  value ="' + model.id + '"/>' +
                '<input type="hidden" name=list[' + this.formIndex + '].fieldName  value ="' + model.fieldName + '"/>' +
                '<label class="col-sm-2 control-label" style="font-size: 12px;margin-left: -8px">' + model.fieldName + "：" + '</label>' +
                '<div class="col-sm-8">';
            if ("list" == type) {
                fieldValue = fieldValue + '<input type="text" class="form-control" name=list[' + this.formIndex + '].fieldValue  value="' + model.fieldValue + '"/>';
            } else {
                fieldValue = fieldValue + '<input type="text" class="form-control" name=list[' + this.formIndex + '].fieldValue  value=""/>';
            }
            oCell.innerHTML = fieldValue + '</div></div>';
            this.formIndex++;
        },
        show: function () {
            this.get();
        },
        showDialog: function (flag, id) {
            this.addRow({});
        },
    });
    $.fn.formList = function (options) {
        var config = $.extend({}, DetailList.DEFAULTS, options);
        var detail = new DetailList(this, config);
        return detail;
    };


})(jQuery)